package com.lsp.lib_toolkit;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.IntDef;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 网络相关工具类
 *
 * @author lsp
 * @created 16/8/16 11:01
 */
public class NetworkUtils {

  public static final int NETWORK_NONE = 0;//没有网络
  public static final int NETWORK_WIFI = 1;//无线网络
  public static final int NETWORK_2G = 2;//2G网络
  public static final int NETWORK_3G = 3;//3G网络
  public static final int NETWORK_4G = 4;//4G网络

  @Deprecated
  public static boolean isWifiOpen(Context mContext) {
    boolean isWifiConnect = false;
    ConnectivityManager cm =
        (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    // check the networkInfos numbers
    NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
    for (int i = 0; i < networkInfos.length; i++) {
      if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
        if (networkInfos[i].getType() == ConnectivityManager.TYPE_MOBILE) {
          isWifiConnect = false;
        }
        if (networkInfos[i].getType() == ConnectivityManager.TYPE_WIFI) {
          isWifiConnect = true;
        }
      }
    }
    return isWifiConnect;
  }

  @IntDef({NetType.WIFI, NetType.CMNET, NetType.CMWAP, NetType.NONE})
  @Retention(RetentionPolicy.SOURCE)
  public @interface NetType {
    int NONE = 0;
    int WIFI = 1;
    int CMWAP = 2;
    int CMNET = 3;
  }

  @Deprecated
  public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager mgr =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] info = mgr.getAllNetworkInfo();
    if (info != null) {
      for (int i = 0; i < info.length; i++) {
        if (info[i].getState() == NetworkInfo.State.CONNECTED) {
          return true;
        }
      }
    }
    return false;
  }

  @Deprecated
  public static boolean isNetworkConnected(Context context) {
    if (context != null) {
      ConnectivityManager mConnectivityManager =
          (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
      if (mNetworkInfo != null) {
        return mNetworkInfo.isAvailable();
      }
    }
    return false;
  }

  /**
   * 这里只是判断网络是否有效  不一定是connected。
   */
  @Deprecated
  public static boolean isWifiConnected(Context context) {
    if (context != null) {
      ConnectivityManager mConnectivityManager =
          (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo mWiFiNetworkInfo =
          mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
      if (mWiFiNetworkInfo != null) {
        return mWiFiNetworkInfo.isAvailable();
      }
    }
    return false;
  }

  @Deprecated
  public static boolean isMobileConnected(Context context) {
    if (context != null) {
      ConnectivityManager mConnectivityManager =
          (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo mMobileNetworkInfo =
          mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
      if (mMobileNetworkInfo != null) {
        return mMobileNetworkInfo.isAvailable();
      }
    }
    return false;
  }

  @Deprecated
  private static int getConnectedType(Context context) {
    if (context != null) {
      ConnectivityManager mConnectivityManager =
          (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
      if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
        return mNetworkInfo.getType();
      }
    }
    return -1;
  }

  /**
   * 打开网络设置界面
   * 3.0以下打开设置界面
   */
  @Deprecated
  public static void openWirelessSettings(Context context) {
    if (android.os.Build.VERSION.SDK_INT > 10) {
      context.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
    } else {
      context.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
    }
  }

  /**
   * 获取当前网络类型
   *
   * @return 0：没有网络 1：WIFI网络 2：WAP网络 3：NET网络
   */
  @Deprecated
  public static @NetType int getNetworkType(Context mContext) {
    int netType = NetType.NONE;
    NetworkInfo networkInfo = null;
    try {
      ConnectivityManager connectivityManager =
          (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
      networkInfo = connectivityManager.getActiveNetworkInfo();
    } catch (SecurityException e) {
      return netType;
    }
    if (networkInfo == null) {
      return netType;
    }
    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_MOBILE) {
      String extraInfo = networkInfo.getExtraInfo();
      if (!TextUtils.isEmpty(extraInfo)) {
        if (extraInfo.toLowerCase().equals("cmnet")) {
          netType = NetType.CMNET;
        } else {
          netType = NetType.CMWAP;
        }
      }
    } else if (nType == ConnectivityManager.TYPE_WIFI) {
      netType = NetType.WIFI;
    }
    return netType;
  }

  /**
   * 网络状态判断
   *
   * @return NETWORK_WIFI   无线
   * NETWORK_NONE   没网
   * NETWORK_2G     2G网络
   * NETWORK_3G     3G网络
   * NETWORK_4G     4G网络
   */
  @Deprecated
  public static int getDetailNetWorkType(Context context) {
    ConnectivityManager manager =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected()) {
      String type = networkInfo.getTypeName();

      if (type.equalsIgnoreCase("WIFI")) {
        return NETWORK_WIFI;
      } else if (type.equalsIgnoreCase("MOBILE")) {
        NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobileInfo != null) {
          switch (mobileInfo.getType()) {
            case ConnectivityManager.TYPE_MOBILE:// 手机网络
              switch (mobileInfo.getSubtype()) {
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_B:
                case TelephonyManager.NETWORK_TYPE_EHRPD:
                case TelephonyManager.NETWORK_TYPE_HSPAP:
                  return NETWORK_3G;
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN:
                  return NETWORK_2G;
                case TelephonyManager.NETWORK_TYPE_LTE:
                  return NETWORK_4G;
                default:
                  return NETWORK_NONE;
              }
          }
        }
      }
    }
    return NETWORK_NONE;
  }
}
